package org.csu.lestore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csu.lestore.api.common.CommonResponse;
import org.csu.lestore.api.entity.Addr;
import org.csu.lestore.api.persistence.AddrMapper;
import org.csu.lestore.api.service.AddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("addrService")
public class AddrServiceImpl implements AddrService {
    @Autowired
    private AddrMapper addrMapper;

    @Override
    public CommonResponse<Addr> getDefaultAddrByUserId(int userId) {
        QueryWrapper<Addr> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("status",1);

        Addr addr= addrMapper.selectOne(queryWrapper);
        if(addr==null){
            return CommonResponse.createForError("根据账号获取用户默认地址信息失败或无默认地址");
        }
        return CommonResponse.createForSuccess(addr);
    }

    @Override
    public CommonResponse<Addr> setDefaultAddr(int userId, int addrId) {
        Addr defaultAddr = getDefaultAddrByUserId(userId).getData();
        if(defaultAddr!=null){
            QueryWrapper<Addr> queryWrapper1=new QueryWrapper<>();
            defaultAddr.setStatus(false);
            queryWrapper1.eq("addr_id",defaultAddr.getAddrId());
            addrMapper.update(defaultAddr,queryWrapper1);
        }
        Addr newDefaultAddr = getAddr(addrId).getData();
        newDefaultAddr.setStatus(true);
        QueryWrapper<Addr> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("addr_id",addrId);
        addrMapper.update(newDefaultAddr,queryWrapper2);
        return CommonResponse.createForSuccess("设置默认地址成功",newDefaultAddr);
    }

    @Override
    public CommonResponse<Addr> getAddr(int addrId) {
        QueryWrapper<Addr> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("addr_id",addrId);

        Addr addr= addrMapper.selectOne(queryWrapper);
        if(addr==null){
            return CommonResponse.createForError("根据地址Id获取地址信息失败");
        }
        return CommonResponse.createForSuccess(addr);
    }

    @Override
    public CommonResponse<IPage<Addr>> getAddrListInPage(int curPage, int size) {
        IPage<Addr> addrPage = new Page<>(curPage,size);
        addrMapper.selectPage(addrPage,null);
        return CommonResponse.createForSuccess(addrPage);
    }

    @Override
    public CommonResponse<IPage<Addr>> getAddrListInPage(int curPage, int size, int userId) {
        IPage<Addr> addrPage = new Page<>(curPage,size);
        QueryWrapper<Addr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        addrMapper.selectPage(addrPage,queryWrapper);
        return CommonResponse.createForSuccess(addrPage);
    }

    @Override
    public CommonResponse<Addr> updateAddr(Addr addr) {
        QueryWrapper<Addr> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("addr_id",addr.getAddrId());

        addrMapper.update(addr,queryWrapper);
        return CommonResponse.createForSuccessMessage("修改地址成功");
    }

    @Override
    public CommonResponse<Addr> insertAddr(Addr addr) {
        addrMapper.insert(addr);

        QueryWrapper<Addr> addrQueryWrapper = new QueryWrapper<>();
        addrQueryWrapper.eq("user_id",addr.getUserId());
        if(addrMapper.selectCount(addrQueryWrapper)==1){
            setDefaultAddr(addr.getUserId(),addr.getAddrId());
        }

        return CommonResponse.createForSuccessMessage("新增地址成功");
    }

    @Override
    public CommonResponse<Addr> deleteAddr(int addrId) {
        QueryWrapper<Addr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("addr_id",addrId);

        addrMapper.delete(queryWrapper);
        return CommonResponse.createForSuccessMessage("删除地址成功");
    }
}
